<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

// ******************调用微博相关文件*****************************
			session_start();  //任何时候，页面使用session，开头必须有此句声明。
			require_once(APP_PATH."config.php" );
			require_once(APP_PATH."weibooauth.php" );
// ******************调用微博相关文件*****************************

class main {
	function __construct (){
		$this->url = getUrl();
		$action = $this->url['action'];
		if($action == 'upload'){
			$this->uploadPhoto();
		}elseif($action == 'editphotos'){
			$this->editPhotos();
		}elseif($action == 'reply'){
			$this->reply();			
		}elseif(is_numeric($action)){
			$this->showAction();
		}else{
			$this->defaultAction();
		}
		exit();
	}
	function defaultAction(){
		exit('hsi');
	}

	function showAction(){
		require_once(APP_PATH."model/photos.php" );
		$db  = getDB();
		$data = new photosModel($db);
		$id = $this->url['action'];		
		$photo = $data->getPhoto($id);	
		empty($photo) && pageError();
		$photo['addtime'] = date('Y-m-d H:i',$photo['addtime']);
		$movie = $data->getMovie($photo['mid']);		
		$movie['mid'] = $movie['id'];
		$movie['mtitle'] = $movie['title'];
		$user = $data->getUser($photo['uid']);			
		$tpl = new Template("photo/main.htm");
		$tpl->assign($movie);
		$tpl->assign($photo);
		$tpl->assign($user);

		//reply
		$condition['table'] = 'movie_photo_reply';
		$condition['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$condition['rows'] = '20';
		$condition['condition'] = " pid = '$id' ";
		$condition['order'] = 'addtime';
		$replyList = $data->getList($condition);	
		foreach($replyList as $k => $v ){
			$uids .= "'".$v['uid']."',";
		}
		if($uids){
			$membertmp = $data->getMembers(substr($uids,0,-1));
			foreach($membertmp as $k => $v ){
				$members[$v['uid']] =  $v;
			}
		}
		foreach($replyList as $k => $v ){
			$uid = $v['uid'];
			$replyList[$k]['addtime'] = date("Y-m-d  H:i:s",$v['addtime']);
			$replyList[$k]['username'] = $members[$uid]['username'];
			$replyList[$k]['avatars'] = empty($members[$uid]['avatars']) ? 'default.gif' : $members[$uid]['avatars'];
		}
		$tpl->assign('replyList',$replyList);
		$condition['pageUrl'] = '/photos/'.$id.'/page/';
		$condition['pageTotal']	= $data->total($condition);
		$tpl->assign("pages", makePage($condition));


		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function uploadPhoto(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$movie = $data->getMovie($id);
		//de($movie);
		empty($movie) && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		if(isPost()){			
			$title = htmlClear(trim($_POST['title']));
			$rating = intval(trim($_POST['rev_rating']));
			$content = txt2Html($_POST['content']);
			$uid = $user['uid'];
			if (!is_numeric($rating) || $rating > 5){
				message('您的评分数不正确');
			}
			if (!checkLen($title,6,200)){
				message('您输入的标题长度不合格');
			}
			global $timestamp,$onlineip;		
			$review_id = $data->addReview($id,$uid,$rating,$title,$content,$onlineip,$timestamp);
			if(!empty($review_id)){
				$data->updateMovieReviewNum($id,$rating);
				message('','/review/'.$review_id);
			}else{
				message('发表影评失败，请重新发表。');
			}
		}else{
			$tpl = new Template("photo/upload.htm");
			$movie['logintip'] = $user['logintip'];
			$tpl->assign($movie);
			$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
			$tpl->output();
		}		
		
	}

	function editPhotos(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$movie = $data->getMovie($id);
		//de($movie);
		empty($movie) && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$uid = $user['uid'];
		if(isPost()){
			$photos = $_POST['photos-desc'];
			foreach($photos as $k => $v) {
				$title = htmlClear(trim($v));
				if (!checkLen($phototitle,0,200)){
					message('您输入的标题太长了。');
				}
				$update_photos = $data->updatePhotos($k,$id,$uid,$title);
			}
			message('','/movie/'.$id);
		}else{
			$tpl = new Template("photo/editphotos.htm");
			$movie['logintip'] = $user['logintip'];
			$tpl->assign($movie);
			$sql = "SELECT * FROM movie_photo WHERE mid = '$id' AND uid = '$uid' AND tmp ='1' ";
			$db->query($sql);
			$photos = $db->fetchAll();
			//de($photos);
			$tpl->assign('photos',$photos);
			$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
			$tpl->output();
		}		
		
	}

	function reply(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$id = intval(trim($id));

		$photo = $data->getPhoto($id);
		empty($photo) && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$uid = $user['uid'];
		if(isPost()){
			$content = htmlClear(trim($_POST['content']));	
			$mid = $photo['mid'];			
			if (!checkLen($content,3,200)){
				message('您输入的内容太长或太短。');
			}
			global $timestamp,$onlineip;		
			$is_reply = $data->addPhotoReply($id,$mid,$uid,$content,$onlineip,$timestamp);
			
			
			
			//***********增加： 回应图片同步到微博××××××××××××××××××××××××××××××××××××××××××××
			if ($_POST['checkbox'] =="check")
			{
			$photourl="http://".$_SERVER['SERVER_NAME'].$_POST['photourl'];    //电影图片url
			$mtitle=$_POST['mtitle'];    //电影标题
			$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
			
			$url = "http://".$_SERVER['SERVER_NAME']."/photos/".$id;  //url在新浪的长度是10个中文字符
			$weibo="我回应<".$mtitle.">的图片: ". $content;
		    $weibo= cut_str($weibo, 128);   //这函数的中文字符数量，新浪极限是138, 超链接占9， 因此这里截取 128
			$weibo = $weibo."-".$url;	
			$rr = $c->upload($weibo, $photourl);
		//	echo $photourl; exit;
            }
			//***********增加： 回应图片同步到微博×××××××××××××××××××××××××××××××××××××××××	
			
			
			if(!empty($is_reply)){
				$data->updatePhotoReplyNum($id);
				message('','/photos/'.$id);
			}else{
				message('发表图片回应失败，请重新发表。');
			}
		}else{
			pageError();
		}		
		
	}

}
?>
